package com.casic.ops.runner.pipeline.node.command;

import com.alibaba.fastjson.JSON;
import com.casic.ops.entity.domain.MachineInfoDO;
import com.casic.ops.entity.dto.pipeline.datastructure.Logger.NodeLogger;
import com.casic.ops.entity.dto.pipeline.datastructure.context.DefaultResult;
import com.casic.ops.entity.dto.pipeline.datastructure.context.Result;
import com.casic.ops.entity.dto.pipeline.datastructure.executecontext.PipelineExecuteContext;
import com.casic.ops.entity.dto.pipeline.datastructure.node.PipelineNodeConfigInfo;
import com.casic.ops.runner.pipeline.node.AbstractExecutableNode;
import com.casic.ops.runner.pipeline.nodeconfiginfo.build.JavaBuildNodeConfigInfo;
import com.casic.ops.runner.pipeline.nodeconfiginfo.command.ArtificialStickingPointConfigInfo;
import com.casic.ops.service.api.MachineInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 人工卡点执行类
 * @author cww
 * @date 2022/6/23
 */
@Service
@Slf4j
public class ArtificialStickingPointExecutableNode extends AbstractExecutableNode {

    @Resource
    private MachineInfoService machineInfoService;

    @Override
    public Result execute(PipelineExecuteContext context,String nodeUuid) {
        int statusCode=-1;
//      NodeLogger logger = NodeExecuteLoggerFactory.getNodeExecuteLogger().setExecuteStartTime(new Date());
        NodeLogger logger = (NodeLogger) context.getAttributes(nodeUuid+"logger");
        logger.setNodeName("人工卡点");
        log.info(getName());
        PipelineNodeConfigInfo nodeConfigInfo = (PipelineNodeConfigInfo)context.getAttributes(nodeUuid);
        DefaultResult defaultResult = new DefaultResult();
        try {
            log.info("attr : {}", nodeConfigInfo.getInfo());
            System.out.println("节点信息:" + nodeConfigInfo.getInfo());
            //将节点的配置信息反编译成对象
            ArtificialStickingPointConfigInfo artificialStickingPointConfigInfo = JSON.parseObject(nodeConfigInfo.getInfo(), ArtificialStickingPointConfigInfo.class);
            System.out.println("构建脚本" + artificialStickingPointConfigInfo.getCommandScript());
            String script = artificialStickingPointConfigInfo.getCommandScript();
            //如果machineId为0，则说明该节点没有配置机器，则使用开始节点的机器
            long machineId = 0;
            if (artificialStickingPointConfigInfo.getMachineId() == 0) {
                machineId = (long) context.getAttributes("machineId");
            } else {
                machineId = artificialStickingPointConfigInfo.getMachineId();
            }
            //获取机器
            MachineInfoDO machineInfoDO = machineInfoService.selectById(machineId);
            statusCode = shell(logger, context, machineInfoDO,
                    "echo \"人工卡点\"",
                    script
            );
        }catch (Exception e){
            String errorMessage="该节点配置信息为空，请先配置该节点信息"+"\r\n";
            ErrorHandle(e,logger,errorMessage);
        }
        if(statusCode==0) {
            log.info("{}节点执行完成", getName());
        }else {
            log.error("{}节点执行失败",getName());
        }
        defaultResult.add("status",statusCode);
        return defaultResult;
    }
    /**
     * 此方法返回的name是和pipeline_node_template里面节点的name是对应的，如果要修改，请同时去数据库里面，把对应的一条数据的name也改成一致的
     * @return
     */
    @Override
    public String getName() {
        return "MANUAL_CLAMPING_POINT";
    }
}
